#-*- coding: utf-8 -*-

from cocos.menu import *
from cocos.layer import MultiplexLayer

class CMenu(MultiplexLayer):
	m_Name="Default Menu"
	#菜单选项
	m_Items={}

	def __init__(self,iMenuID):
		self.m_ID=iMenuID
		self.m_Menus={}
		self.m_MenuIdx=-1
		self.PrepareMenu(self.m_Items)
		menuList=self.MenuList()
		MultiplexLayer.__init__(self,*menuList)
		self.switch_to(0)

	def PrepareMenu(self,dItems,iParent=0):
		self.m_MenuIdx+=1
		iMenuIdx=self.m_MenuIdx
		itemList=[]
		for idx,dItemInfo in dItems.iteritems():
			sTitle=dItemInfo["text"]
			if "items" in dItemInfo:
				iSubMenuIdx=self.PrepareMenu(dItemInfo["items"],iMenuIdx)
				oItem=MenuItem(sTitle,self.Switch,iSubMenuIdx)
			elif "return" in dItemInfo:
				oItem=MenuItem(sTitle,self.Switch,iParent)
			else:
				oItem=MenuItem(sTitle,self.Click,idx)
			itemList.append(oItem)
		oMenu=Menu(self.m_Name)
		oMenu.menu_halign=RIGHT
		oMenu.menu_valign=TOP
		oMenu.menu_hmargin=50
		oMenu.menu_vmargin=50
		oMenu.create_menu(itemList)
		self.m_Menus[iMenuIdx]=oMenu
		return iMenuIdx

	def MenuList(self):
		menuList=[]
		iLen=len(self.m_Menus)
		for i in range(iLen):
			oMenu=self.m_Menus[i]
			menuList.append(oMenu)
		return menuList

	#切换菜单
	def Switch(self,iMenu):
		print 'switch_to',iMenu
		self.switch_to(iMenu)

	#子类继承实现
	def Click(self,iItem):
		pass
	
Modules={}
import m0001
Modules[1]=m0001
